﻿function HandlePageBackendPackage(gui, operator, now, options) {
  this._gui = gui;
  this._operator = operator;
  this._now = now;
  this._options = options;
  this._popupBox = new PopupBox();
  this._popupBox2 = new PopupBox2();
  
  this._createElements();
};

HandlePageBackendPackage.prototype._createElements = function() {
  this._loadData();
};

HandlePageBackendPackage.prototype._loadData = function() {
  this._package = null;
  this._samples = null;
  this._retrievePackages();
  this._retrieveSamples();
};

HandlePageBackendPackage.prototype._verifyData = function() {
  if (this._package && this._samples) {
    this._updateElements();
  }
};

HandlePageBackendPackage.prototype._retrievePackages = function(page, condition, date, from, to, pos) {
  var _self = this;
  if (this._options.id) {
    /*new RequestUtils()._read('package', null, 'd.oid = ' + this._options.id, null, null, null, null, function(result, params) { _self._package = (result.data.length == 1) ? result.data[0] : null;
                                                                                                                                _self._verifyData.call(_self);
                                                                                                                              }, null);
*/
   var args = '&oid=' + this._options.id; 
  new RequestUtils()._mysql('v1package_s', args, function(result, params) { _self._package = (result.data.length == 1) ? result.data[0] : null;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});  

  } else {
    this._package = v1package.instance();
    this._package.packageItems = [];
    this._verifyData();
  }
};

HandlePageBackendPackage.prototype._retrieveSamples = function(page, condition, date, from, to, pos) {
  var _self = this;
  /*new RequestUtils()._read('sample', null, null, null, null, null, null, function(result, params) { _self._samples = result.data;
                                                                                                    _self._verifyData.call(_self);
                                                                                                  }, null);
*/

  new RequestUtils()._mysql('v1sample_s', null, function(result, params) { _self._samples = result.data;
                                                                          _self._parameters = result;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php',pos: (pos) ? pos : null }); 
};

HandlePageBackendPackage.prototype._updateElements = function() {
  var table, tr, td, _self = this;
  DlmanageUtils.clear(this._gui);
  
  this._gui.makeup.options[this._gui.makeup.options.length] = new Option('');
  for (var i = 0, il = MakeupType.array().length; i < il; i++) {
    var type = MakeupType.array()[i];
    option = new Option(type);
    this._gui.makeup.options[this._gui.makeup.options.length] = option;
    if (option.text == this._package.makeup) {
      this._gui.makeup.selectedIndex = this._gui.makeup.options.length - 1;
    }
  }
  this._gui.type.options[this._gui.type.options.length] = new Option('');
  for (var i = 0, il = PackageType.array().length; i < il; i++) {
    var type = PackageType.array()[i];
    option = new Option(type);
    this._gui.type.options[this._gui.type.options.length] = option;
    if (option.text == this._package.type) {
      this._gui.type.selectedIndex = this._gui.type.options.length - 1;
    }
  }
  this._gui.orderType.options[this._gui.orderType.options.length] = new Option('');
  for (var i = 0, il = OrderType.array().length; i < il; i++) {
    var type = OrderType.array()[i];
    option = new Option(type);
    this._gui.orderType.options[this._gui.orderType.options.length] = option;
    if (option.text == this._package.orderInc) {
      this._gui.orderType.selectedIndex = this._gui.orderType.options.length - 1;
    }
  }
  
  this._gui.name.value = this._package.name;
  this._gui.hours.value = this._package.hours;
  this._gui.persons.value = this._package.persons;
  this._gui.price.value = this._package.price;
  
  this._gui.name.onchange = function() { _self._package.name = this.value; };
  this._gui.hours.onchange = function() { _self._package.hours = this.value; };
  this._gui.persons.onchange = function() { _self._package.persons = this.value; };
  this._gui.makeup.onchange = function() { _self._package.makeup = this.options[this.selectedIndex].text; };
  this._gui.type.onchange = function() { _self._package.type = this.options[this.selectedIndex].text; };
  this._gui.orderType.onchange = function() { _self._package.orderInc = this.options[this.selectedIndex].text; };
  this._gui.price.onchange = function() { _self._package.price = this.value; };
  
  this._gui.addProduct.onclick = function() { var packageItem = v1packageitem.instance();
                                              packageItem.sample = _self._samples[0];
                                              packageItem.quantity = 1;
                                              packageItem.total = packageItem.sample.price;
                                              packageItem.otherId = MiscUtils.unique();
                                              packageItem.selects = [];
                                              for (var i = 0, il = packageItem.sample.attributes.length; i < il; i++) {
                                                var attribute = packageItem.sample.attributes[i];
                                                DlmanageUtils.packageChoose(packageItem, attribute, attribute.options[0]);
                                              }
                                              _self._addPurchase.call(_self, packageItem);
                                            };
  this._gui.save.onclick = function() { var str = '';
                                        if (_self._package.name == '') {
                                          str += '套餐名称, ';
                                        }
                                        if (_self._package.type == '') {
                                          str += '套餐类型, ';
                                        }
                                        if (_self._package.orderInc == '') {
                                          str += '订单类型, ';
                                        }
                                        if (_self._package.name == '' || _self._package.type == '' || _self._package.orderInc == '') {
                                          window.alert(str.substring(0, str.length-2) + ' 不能为空');
                                        } else {
                                        //function() { history.back(); }
                                          new RequestUtils()._mwrite('v1package', [_self._package], [], function() { history.back(); }, null);
                                        }
}
  this._gui.cancel.onclick = function() { history.back(); };
  new TablePurchases(this._gui.purchases, this._package.packageItems, { parent: this._package, samples: this._samples, callbackFunc: function() { _self._updateElements.call(_self); }, packageOnly: true, isPackage: true });
};

HandlePageBackendPackage.prototype._addPurchase = function(packageItem) {
  var div, _self = this;
  var func1 = function() { _self._popupBox2._close();
                           _self._package.packageItems[_self._package.packageItems.length] = packageItem;
                          
                           _self._updateElements.call(_self);
                         };
  var func2 = function() { _self._popupBox2._close();
                           _self._updateElements.call(_self);
                         };
  var div = document.createElement('div');
  MiscUtils.dialog(this._popupBox2, null, div, func1, func2, {t: 'Add Purchase', width: 800, height: 50});
  var iContainerPurchase = new IContainerPurchase(div, packageItem, { callbackFunc: function() { iContainerPurchase._createElements(); }, parent: this._package, samples: this._samples, packageOnly: true, isPackage: true, isAdd: true });
};
